var express = require('express');
var router = express.Router();
var { uploadFiles } = require('../utils/handleFiles');

const {getInsures,addInsures,deleteInsuresById,getInsuresId,updateInsures}=require('../service/insuresService');


// 获取产品
router.get('/getInsures', async(req, res, )=> {
 
   const data = await getInsures(req.query);
   res.send(data);
});



// 新增产品
router.post('/addInsures',async(req,res)=>{
  const{name,payType,suitableFor,renew,policy,price,value,imageUrl}=req.body;
     res.send(await addInsures({name,payType,suitableFor,renew,policy,price,value,imageUrl}));
})


// 删除产品
router.post('/deleteInsuresById',async(req,res)=>{
  
   const{id}=req.body;
      res.send(await deleteInsuresById({id}));
 })



// 上传图片
 router.post('/imagesInsures',function(req,res,next){
   const images =  uploadFiles({
      // 图片上传成功储存的路径
         path:'./public/temp',
         key:'file',
         size:5000
     });
     
     images(req,res,(err)=>{
             if(err){
              console.log('图片上传失败',err);
             }else{
                 console.log('图片上传成功');
                 console.log(req.files);
                 res.send({
                     message:'图片上传成功',
                     code:1,
                     data:req.files[0].filename
                 })
             }
     });
});








// 修改获取值
router.get('/getInsuresId', async(req, res)=> {
   const data = await getInsuresId(req.query);
   res.send(data);
});



// 修改发送请求
router.post('/updateInsures', async(req,res)=> {
   res.send(await updateInsures(req.body));
});








module.exports = router;